package com.hnqcgc.controller;

import com.hnqcgc.service.IProductService;
import com.hnqcgc.service.ProductServiceImpl;
import com.hnqcgc.vo.TProduct;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

// 提供地址栏访问路径 http://localhost:8080/home
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取服务层数据
        IProductService productService=new ProductServiceImpl();
        List<TProduct> productlist= null;
        try {
            productlist = productService.getProductList(8);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //2 存储于session
        req.getSession().setAttribute("productList",productlist);
        //3 重定向到index.jsp 页面
        // 首页呈现数据 商品推荐
        resp.sendRedirect("/index.jsp");
    }
}
